<template>
    <div>
        <ToolBar>
<!-- 
            <div>
                <el-input placeholder="请输入评论编号" size="small" style="width: 140px" v-model="searchParams.title" clearable>
                </el-input> -->
                <!-- <el-select v-model="searchParams.type" clearable style="width: 140px" placeholder="请选择用户类型"
                    size="small">
                    <el-option v-for="item in goodsTypeData" :key="item.id" :label="item.typename" :value="item.id">
                    </el-option>
                </el-select> -->
                <!-- <el-button type="success" size="small" @click="refresh()">查询</el-button>
                <el-button type="warning" size="small" @click="clearSearchParams()">重置</el-button>
            </div> -->
        </ToolBar>
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="user.username" label="用户名"></el-table-column>
            <el-table-column prop="product.name" label="产品名称" ><span v-if="this.productId != -1">{{this.productName}}</span></el-table-column>
            <el-table-column prop="rate" label="评价"></el-table-column>
            <el-table-column prop="content" label="内容"></el-table-column>
            <el-table-column prop="createDate" label="创建日期"></el-table-column>

        </el-table>
        <el-pagination background :page-size="pageSize" layout="prev, pager, next" :total="total"
            @current-change="changePage"></el-pagination>
        <!--    <Pagination-->
        <!--      :params="searchParams"-->
        <!--      :requestFunc="requestFunc"-->
        <!--      ref="pagination"-->
        <!--      @returnData="returnData"-->
        <!--    />-->
        <Edit :showEditDialog="showEditDialog" @close="showEditDialog = false" @success="refresh()" />
    </div>
</template>

<script>

import { resetObject } from "@/utils/common";
import { todelUser } from "@/api/user/userlist";
import { comment } from "@/api/commentManage/comment";
import { getCommentById} from "@/api/commentManage/comment";
//import { getCommentByUserId} from "@/api/commentManage/comment"
export default {
    data() { //对评论表数据初始化
        return {
            pageSize: 5, //每页显示5条
            total: 0, //总条数
            pageNo: 1, //默认当前页码第1页
            searchParams: {
                title: "",
                type: ""
            },
            productId:-1,
            showEditDialog: false,
            tableData: [],
            goodsTypeData: [],
            id: null,
            stateValue: 1
        };
    },
    created() {
        
        this.getParams();
        this.initData(); //页面加载就初始化用户数据，默认选第一页的数据
       
    },
    methods: {
        getParams() {
            if(this.$route.query.productId != null){
            this.productId = this.$route.query.productId;
            console.log("接受的商品id" + this.productId)
            }
        },
       
        changePage(val) {
            //val:改变页码当前的页码
            this.pageNo = val;
            this.initData(); //根据新的页码选取分页数据
        },
        initData() {
            if(this.productId == -1){
                console.log("comment")
                console.log(this.productId)
            comment({
                pageNo:this.pageNo,  //由于加了分页器，需要先post 页码和页面大小
                pageSize:this.pageSize
            }).then(resp=>{
                console.log(resp);
                this.tableData = resp.records; //将返回数据给表格
                this.total = resp.total; //?
            })
            }
            else{
                 console.log("getById")
                 console.log(this.productId)
                getCommentById({
                    productId:this.productId,
                    pageNo:this.pageNo,  //由于加了分页器，需要先post 页码和页面大小
                pageSize:this.pageSize
                }).then(resp=>{
                    this.tableData = resp.records; //将返回数据给表格
                this.total = resp.total; //?
                })
            }
           

        },
        editItem(id) {
            this.id = id;
            this.showEditDialog = true;
        },

        refresh() {
            this.initData();
        },
        returnData(pageList) {
            this.tableData = pageList.list;
        },
        clearSearchParams() {
            resetObject(this.searchParams);
            this.refresh();
        },
        updateState(id, newstate) {
            if (newstate == 0) {
                //删除
                this.$confirm("确定删除?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        console.log(id);
                        todelUser({ "id": id });
                        this.initData();
                    })
                    .catch(() => { });
            }
        },
        updateStateApi(id, newstate) {
            console.log(id, newstate);

            // row.d = 0;
            // updateStatus({ id: row.id})
            //   .then(r => {
            //     this.$message({
            //       type: "success",
            //       message: "操作成功!"
            //     });
            //     this.refresh();
            //   })
            //   .catch(() => {});
        }
    },


};
</script>